package node;

import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.util.Arrays;
import java.util.Scanner;

import remote.RemoteDistributedHashMap;

import log.Logger;
import messaging.InterNodeMessage;
/**
 * This class is the landing point for removal of actual node.
 *  This will interactively ask the user for the node to remove.
 * @author Team4
 * @version Spring 2013
 *
 */

public class LaunchRemoveNode
{
  /**
   * Registry name.
   */
  private static String REGISTRY_NAME = "RemoteRegistry";

  /**
   * Private constructor to prevent instantiation.
   */
  public LaunchRemoveNode()
  {
    // do nothing
  }
  /**
   * This method is called from the NodeManager that will initialize all the
   * variables in the NodeManager and add the new node information to it.
   * The new node also exists alongside the old node in the rmi registry 
   * with different names.
   * @throws NotBoundException 
   * @throws InterruptedException 
   */
  public int Remove_Node() throws NotBoundException, InterruptedException
  {
    try
    {
      System.out.println(" list of nodes currently in the registry are  " +
          Arrays.toString(NodeManager.registry.list()));
      System.out.println(" Select a actual node no to remove  - ");
      final Scanner scanner = new Scanner(System.in);
      String node_name = scanner.nextLine();
      if (node_name.trim().isEmpty())
      {
        System.out.println("Hmm, I didn't get node name");
        System.exit(1);
      }
      if (node_name.trim().endsWith("V") || node_name.trim().endsWith("A"))
      {
        node_name = node_name.substring(0, node_name.length() - 1);
      }
      final int node_num = Integer.parseInt(node_name);
      /*if (this.rmi_NodeFind(node_num).endsWith("V"))
      {
        System.out.println(" please give a actual node no");
        System.exit(1);
      }*/
      
      if(Arrays.toString(NodeManager.registry.list()).contains(node_num + "V"))
      {
        System.out.println(" please give a actual node no");
        System.exit(1);
      }
      /*NodeManager.actual_node_number = node_num;
      NodeManager.LOGGER_FILE_NAME = "LOG-" + NodeManager.actual_node_number + ".txt";
      NodeManager.total_nodes_chord = NodeManager.registry.list().length;
      Logger.writeContent(NodeManager.LOGGER_FILE_NAME, " the node " +
          node_num + " is going to be removed from the chord.");
      Logger.out(" the node " + node_num + " is going to be removed from the chord.");
      Logger.writeContent(NodeManager.LOGGER_FILE_NAME, 
                          "the total nodes in the current chord is " + 
                           NodeManager.total_nodes_chord);
     /* NodeManager.remoteObjectCreation(node_num, 
                                       NodeManager.HOST,
                                       NodeManager.PORT, 
                                       NodeManager.REMOTE);*/
      Logger.out(" List of nodes temporarily in registry are - " +
          Arrays.toString(NodeManager.registry.list()));
      return node_num;
    }
    catch (final RemoteException e)
    {
      //Logger.writeContent(NodeManager.LOGGER_FILE_NAME, "A problem occurred: " + e);
      System.exit(1);
      return -1;
    }
  }

  

}
